<?php

use Illuminate\Database\Seeder;
use App\Models\ForumCategory;
use App\Models\User;
use App\Models\ForumTopic;
use Carbon\Carbon;
class ForumTopicsTableSeeder extends Seeder
{
    public function run()
    {
          // 所有用户 ID 数组，如：[1,2,3,4]
        $user_ids = User::all()->pluck('id')->toArray();

        // 所有分类 ID 数组，如：[1,2,3,4]
        $category_ids = ForumCategory::all()->pluck('id')->toArray();

        // 获取 Faker 实例
        $faker = app(Faker\Generator::class);
        ini_set('memory_limit', '6024M');
        $topics = factory(ForumTopic::class)
                        ->times(100)
                        ->make()
                        ->each(function ($topic, $index)
                            use ($user_ids, $category_ids, $faker)
        {

            // 从用户 ID 数组中随机取出一个并赋值
            $topic->user_id = $faker->randomElement($user_ids);

            // 话题分类，同上
            $topic->category_id = $faker->randomElement($category_ids);

            // 回复者，同上
            $topic->last_reply_user_id = $faker->randomElement($user_ids);
            if($index==0)
            {
                $topic->title="[北京][20k-40k] 全球社交 App Holla 公司 高级 C++ 开发工程师";
            }
        });
        ForumTopic::insert($topics->toArray());

    }

}
